﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;

namespace YJKTextDimension.DoorWindowDim
{
	public class ElementGroupComparer : IComparer<List<Element>>
	{
		public ElementGroupComparer(XYZ vecViewOriention)
		{
			this.m_vecViewOriention = vecViewOriention;
		}

		public int Compare(List<Element> lstElemsFirst, List<Element> lstElemsSecond)
		{
			if (this.m_vecViewOriention.IsAlmostEqualTo(new XYZ(1.0, 0.0, 0.0)))
			{
				this.m_dFirstElem = (lstElemsFirst.ElementAt(0).Location as LocationPoint).Point.Y;
				this.m_dSecondElem = (lstElemsSecond.ElementAt(0).Location as LocationPoint).Point.Y;
				this.m_nRetValue = this.m_dFirstElem.CompareTo(this.m_dSecondElem);
			}
			else if (this.m_vecViewOriention.IsAlmostEqualTo(new XYZ(-1.0, 0.0, 0.0)))
			{
				this.m_dFirstElem = (lstElemsFirst.ElementAt(0).Location as LocationPoint).Point.Y;
				this.m_dSecondElem = (lstElemsSecond.ElementAt(0).Location as LocationPoint).Point.Y;
				this.m_nRetValue = this.m_dSecondElem.CompareTo(this.m_dFirstElem);
			}
			else if (this.m_vecViewOriention.IsAlmostEqualTo(new XYZ(0.0, -1.0, 0.0)))
			{
				this.m_dFirstElem = (lstElemsFirst.ElementAt(0).Location as LocationPoint).Point.X;
				this.m_dSecondElem = (lstElemsSecond.ElementAt(0).Location as LocationPoint).Point.X;
				this.m_nRetValue = this.m_dFirstElem.CompareTo(this.m_dSecondElem);
			}
			else if (this.m_vecViewOriention.IsAlmostEqualTo(new XYZ(0.0, 1.0, 0.0)))
			{
				this.m_dFirstElem = (lstElemsFirst.ElementAt(0).Location as LocationPoint).Point.X;
				this.m_dSecondElem = (lstElemsSecond.ElementAt(0).Location as LocationPoint).Point.X;
				this.m_nRetValue = this.m_dSecondElem.CompareTo(this.m_dFirstElem);
			}
			return this.m_nRetValue;
		}

		public XYZ m_vecViewOriention;

		private double m_dFirstElem;

		private double m_dSecondElem;

		private int m_nRetValue;
	}
}
